package casa.depesos.app.com.frag;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.support.toast.ToastUtil;

import casa.depesos.app.com.R;
import casa.depesos.app.com.activity.ExtendActivity;
import casa.depesos.app.com.activity.HowActivity;
import casa.depesos.app.com.activity.WebActivity;
import casa.depesos.app.com.databinding.FragmentRepayBinding;
import casa.depesos.app.com.dlg.RepayDetailDlg;
import casa.depesos.app.com.dlg.TipDlg;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.ExtendLinkBean;
import casa.depesos.app.com.http.bean.RepayDetailBean;
import casa.depesos.app.com.http.data.ExtendLink;
import casa.depesos.app.com.http.data.RepayDetail;
import casa.depesos.app.com.http.req.ExtendLinkReq;
import casa.depesos.app.com.http.req.RepayDetailReq;
import casa.depesos.app.com.it.Refreshable;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;


public class RepayFragment extends BannerBaseFragment implements Refreshable {

    FragmentRepayBinding binding;
    ExtendLink link;
    int status;

    public RepayFragment setStatus(int i) {
        status = i;
        return this;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Report.show("ACTION_TT_001");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentRepayBinding.inflate(inflater, container, false);
        binding.rela.setPadding(0, Utils.getStatusBarHeight(getActivity()), 0, 0);

        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

    private void update() {
        getName(binding.top);
        getUnRead(binding.top);
        getLink(binding.bottom);
        showMsg(binding.msg, binding.scroll);
        getBanner(binding.banner);
        getData();
    }

    private void getData() {
        showLoading();
        new RepayDetailReq().req(new ReqCallback<RepayDetailBean>() {
            @Override
            public void callback(boolean success, RepayDetailBean bean) {
                if (success) {
                    show(bean.info);
                } else {
                    hideLoading();
                }
            }
        });
    }

    int way = 0;

    private void changeWay(int now) {
        if (now == 0) {
            binding.repay.setBackgroundResource(R.drawable.right_btn);
            binding.repay.setTextColor(Color.WHITE);
            binding.payed.setBackgroundResource(R.drawable.green_stroke_bg);
            binding.payed.setTextColor(0xff71D561);
        } else {
            binding.payed.setBackgroundResource(R.drawable.right_btn);
            binding.payed.setTextColor(Color.WHITE);
            binding.repay.setBackgroundResource(R.drawable.green_stroke_bg);
            binding.repay.setTextColor(0xff71D561);
            ToastUtil.showShortToast(getActivity(), "Aún no es compatible");
        }
        way = now;
    }

    private void show(RepayDetail info) {
        getLink(info.no, null);
        if (info.isOverdue) {
            binding.tip.setVisibility(View.VISIBLE);
            binding.tipTv.setText(String.format("Ya está atrasado %d días", info.overDay));
        } else {
            binding.tip.setVisibility(View.INVISIBLE);
        }
        binding.tv.setText(String.format("Monto pendiente a pagar\nantes del %s", Utils.getDay3(info.repayTime + "")));
        binding.s.setVisibility(View.VISIBLE);
        binding.max.setVisibility(View.VISIBLE);
        binding.total.setText(Utils.formatNoFloat(info.remain + "", true));
        binding.take.setVisibility(info.isExtend ? View.VISIBLE : View.GONE);
        binding.take.setOnClickListener(v -> {
            Report.click("ACTION_TT_002");
            Intent i = new Intent(getActivity(), ExtendActivity.class);
            i.putExtra("id", info.no);
            i.putExtra("email", email);
            i.putExtra("detail", info);
            if (link != null) {
                i.putExtra("fee", link.fee);
            }
            startActivity(i);
        });
        binding.max.setOnClickListener(v -> showDetail(info));
        binding.repay.setOnClickListener(v -> {
            Report.click("ACTION_TT_003");
            changeWay(0);
        });
        binding.payed.setOnClickListener(v -> {
            Report.click("ACTION_TT_003");
            ToastUtil.showLongToast(getActivity(), "Todavía no está abierto, estad atentos");
        });
        binding.copy.setOnClickListener(v -> {
            Utils.copy(binding.code.getText().toString(), false);
            new TipDlg().setMsg("Copiado", getString(R.string.tip2)).show(getChildFragmentManager(), "");
            Report.click("ACTION_TT_004");
        });
        binding.help.setVisibility(View.VISIBLE);
        binding.help.setOnClickListener(v -> {
            if (way == 1) {
                Intent i = new Intent(getActivity(), HowActivity.class);
                startActivity(i);
            } else {
                new TipDlg().setMsg("¿Cómo pagar ?", "").show(getChildFragmentManager(), "");
            }
        });
    }

    private void showDetail(RepayDetail info) {
        if(link == null){
            return;
        }
        new RepayDetailDlg().setParam(info, link).show(getChildFragmentManager(), "");
    }

    private void toRepay(String id) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(getActivity(), WebActivity.class);
                i.putExtra("url", link.url);
                i.putExtra("top", true);
                startActivity(i);
            }
        };
        if (link != null) {
            runnable.run();
        } else {
            getLink(id, runnable);
        }
    }

    private void getLink(String id, Runnable runnable) {
        showLoading();
        new ExtendLinkReq(0, id).req(new ReqCallback<ExtendLinkBean>() {
            @Override
            public void callback(boolean success, ExtendLinkBean bean) {
                hideLoading();
                if (success) {
                    link = bean.info;
                    binding.max.setText(Utils.formatNoFloat(link.amount + ""));
                    binding.frame.setVisibility(View.VISIBLE);
                    binding.tv2.setText(Utils.formatNoFloat(link.fee, true));
                    binding.code.setText(link.account);
                    if (runnable != null) {
                        runnable.run();
                    }
                }
            }
        });
    }

    @Override
    public void refresh() {
        update();
    }
}
